﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GE_Database_Manager
{
	public partial class ADAPT_Form : Form
	{

        bool[] fileCheck;
        enum files { waveForm, kinematic, namePlate, malfuntion,dataAttributes,comments };
        public delegate void PassDatabase(object sender);
        public PassDatabase passDatabase;

		public ADAPT_Form()
		{
			InitializeComponent();
            // /////set font and colors for text box /////
            // wave form textbox
            textBox_waveForm.Font = new Font(textBox_waveForm.Font,FontStyle.Italic);
            textBox_waveForm.Text = ".csv file";
            textBox_waveForm.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_waveForm.ForeColor = System.Drawing.Color.Gray;
           
            // gearbox kinematic textbox
            textBox_kinematic.Font = new Font(textBox_kinematic.Font, FontStyle.Italic);
            textBox_kinematic.Text = ".xls file";
            textBox_kinematic.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_kinematic.ForeColor = System.Drawing.Color.Gray;

            // namaplate data textbox        
            textBox_namePlate.Font = new Font(textBox_namePlate.Font, FontStyle.Italic);
            textBox_namePlate.Text = ".pdf file";
            textBox_namePlate.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_namePlate.ForeColor = System.Drawing.Color.Gray;
       
            // wave form textbox
            textBox_malfunction.Font = new Font(textBox_malfunction.Font, FontStyle.Italic);
            textBox_malfunction.Text = ".txt file";
            textBox_malfunction.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_malfunction.ForeColor = System.Drawing.Color.Gray;

            // data attribues textbox
            textBox_dataAttributes.Font = new Font(textBox_dataAttributes.Font, FontStyle.Italic);
            textBox_dataAttributes.Text = ".txt file";
            textBox_dataAttributes.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_dataAttributes.ForeColor = System.Drawing.Color.Gray;

            // comments textbox
            textBox_comments.Font = new Font(textBox_comments.Font, FontStyle.Italic);
            textBox_comments.Text = ".txt file";
            textBox_comments.BackColor = System.Drawing.Color.WhiteSmoke;
            textBox_comments.ForeColor = System.Drawing.Color.Gray;

            // disable textbox.. for now
            textBox_waveForm.ReadOnly = true;
            textBox_kinematic.ReadOnly = true;
            textBox_namePlate.ReadOnly = true;
            textBox_malfunction.ReadOnly = true;
            textBox_comments.ReadOnly = true;

            // set the file check flags
            fileCheck = new bool[] {false,false,false,false,false,false};

		}
        /// <summary>
        ///  this function check if the filename open has the correct extention
        ///  if it does, it stores the path in the given textbox
        /// </summary>
        /// <param name="currentBox"></param>
        /// <param name="fileName"></param>
        /// <param name="ext"></param>
        private bool setTextBox(TextBox currentBox, String fileName, string ext)
        {
            // check for file extension
            if (checkFileExt(fileName, ext))
            {
                setFont(currentBox, FontStyle.Regular, Color.Black);
                currentBox.Text = fileName;
                return true;
            }
            else
            {
                MessageBox.Show("Invalid File Extension.. Expected  " + ext + " file extension");
                setFont(currentBox,FontStyle.Italic,Color.Gray);
                currentBox.Text = ext + " file";
                return false;
            }
        
        }

        private void button_waveForm_Click(object sender, EventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "CSV Files (*.csv) | *csv";


            if (openFileDialog.ShowDialog() == DialogResult.OK) 
            {
                // set the text box if is valid
                if (setTextBox(textBox_waveForm, openFileDialog.FileName, ".csv"))
                    fileCheck[(int)files.waveForm] = true;
                else
                    fileCheck[(int)files.waveForm] = false;
            }

        }

        private void button_kinematic_Click(object sender, EventArgs e)
        {   // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "XLS Files (*.xls*) | *xls*";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // set the text box if is valid
                if (setTextBox(textBox_kinematic, openFileDialog.FileName, ".xls"))
                    fileCheck[(int)files.kinematic] = true;
                else
                    fileCheck[(int)files.kinematic] = false;
            }
        }

        private void button_nameplate_Click(object sender, EventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "PDF Files (*.pdf) | *pdf";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // set the text box if is valid
                if (setTextBox(textBox_namePlate, openFileDialog.FileName, ".pdf"))
                    fileCheck[(int)files.namePlate] = true;
                else
                    fileCheck[(int)files.namePlate] = false;
            }

        }

        private void button_malfunction_Click(object sender, EventArgs e)
        {   // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text Files (*.txt) | *txt";
            
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // set the text box if is valid
                if (setTextBox(textBox_malfunction, openFileDialog.FileName, ".txt"))
                    fileCheck[(int)files.malfuntion] = true;
                else
                    fileCheck[(int)files.malfuntion] = false;
            }
        }

        private void setFont(TextBox targetTextBox, FontStyle newFontStyle, Color newColor)
        {
            targetTextBox.Font = new Font(targetTextBox.Font, newFontStyle);
            targetTextBox.ForeColor = newColor;
        }

        private bool checkFileExt(String fileName, string extension) 
        {
            if (extension == ".xls")
                return System.IO.Path.GetExtension(fileName).Contains ( extension );
            else 
                return System.IO.Path.GetExtension(fileName) == extension;
        }



        private void button_ok_Click(object sender, EventArgs e)
        {
            // check for correct files
            if (checkForFiles())
            {
                if (passDatabase != null)
                {
                    // pass the database to the main form...
                    passDatabase(new ADAPT(textBox_waveForm.Text, textBox_kinematic.Text,
                            textBox_malfunction.Text, textBox_namePlate.Text,textBox_dataAttributes.Text,textBox_comments.Text));
                }

                // get rid of the form
                this.Close();
            }
            else
            {
                MessageBox.Show("Please select valid files...");
            }
        }

        private bool checkForFiles()
        {
            for (int i = 0; i < fileCheck.Length; i++)
                if (!fileCheck[i])
                    return false;

            return true;
        }

        private void button_dataAttributes_Click(object sender, EventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text Files (*.txt) | *txt";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // set the text box if is valid
                if (setTextBox(textBox_dataAttributes, openFileDialog.FileName, ".txt"))
                    fileCheck[(int)files.dataAttributes] = true;
                else
                    fileCheck[(int)files.dataAttributes] = false;
            }

        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_comments_Click(object sender, EventArgs e)
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text Files (*.txt) | *txt";

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // set the text box if is valid
                if (setTextBox(textBox_comments, openFileDialog.FileName, ".txt"))
                    fileCheck[(int)files.comments] = true;
                else
                    fileCheck[(int)files.comments] = false;
            }

        }
	}
}
